﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DoAnOOP.BussinessLayer;
using DevComponents.DotNetBar;

namespace DoAnOOP.NQL
{
    public partial class frmQLMH : DevComponents.DotNetBar.Office2007Form
    {
        public frmQLMH()
        {
            InitializeComponent();
        }

        private void frmQLMH_Load(object sender, EventArgs e)
        {
            clsQLMH mhBUS = new clsQLMH();
            dataGridViewMH.DataSource = mhBUS.LayThongTinMonHoc("");
            KiemTraChinhSua(false);
            dataGridViewMH.Columns[0].HeaderText = "Mã MH";
            dataGridViewMH.Columns[1].HeaderText = "Tên Môn học";
            dataGridViewMH.Columns[2].HeaderText = "Số TC Lý thuyết";
            dataGridViewMH.Columns[3].HeaderText = "Số TC Thực hành";
            dataGridViewMH.Columns[4].HeaderText = "Mã Khoa";
            dataGridViewMH.Columns[5].HeaderText = "Ngày thi";

        }

        #region Hàm trong form
        private void KiemTraChinhSua(bool isEditting)
        {
            bntThem.Enabled = !isEditting;
            btnSua.Enabled = !isEditting;
            btnXoa.Enabled = !isEditting;
            btnXuat.Enabled = !isEditting;
            btnLuu.Enabled = isEditting;
            btnKhongluu.Enabled = isEditting;

            txtMaMH.Enabled = isEditting;
            txtTenMH.Enabled = isEditting;
            txttclt.Enabled = isEditting;
            txttcth.Enabled = isEditting;
            
            pickNgaythi.Enabled = isEditting;
            
            cboKhoa2.Enabled = isEditting;
            
        }

        private void Reset()
        {
           
            txtTenMH.Text = "";
            txtMaMH.Text = "";
            txttcth.Text = "";
            txttclt.Text = "";
            pickNgaythi.Text = "";
            
        }
        private string KiemTraLuu()
        {
            string kiemTra = "";
            if (txtMaMH.Text.Equals(""))
                kiemTra += "Chưa nhập Mã môn học.\n";
            if (txtTenMH.Text.Equals(""))
                kiemTra += "Chưa nhập Tên môn học.\n";
            if (txttcth.Text.Equals(""))
                kiemTra += "Chưa nhập Số TC Thực hành.\n";
            if (txttcth.Text.Equals(""))
                kiemTra += "Chưa nhập Số TC Lý thuyết.";
            return kiemTra;
        }
        #endregion

        int flags;

        private void dataGridViewMH_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int numRecord = e.RowIndex;
                numRecord = e.RowIndex;
                txtMaMH.Text = dataGridViewMH.Rows[numRecord].Cells[0].Value.ToString();
                txtTenMH.Text = dataGridViewMH.Rows[numRecord].Cells[1].Value.ToString();
                txttclt.Text = dataGridViewMH.Rows[numRecord].Cells[2].Value.ToString();
                txttcth.Text = dataGridViewMH.Rows[numRecord].Cells[3].Value.ToString();
                cboKhoa2.Text = dataGridViewMH.Rows[numRecord].Cells[4].Value.ToString();
                pickNgaythi.Text = dataGridViewMH.Rows[numRecord].Cells[5].Value.ToString();
                
            }
            catch (Exception)
            { }
        }

        private void cboKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            clsQLMH mhBUS = new clsQLMH();
            string khoa;

            if (cboKhoa.Text == "Tất cả")
                khoa = "";
            else
                khoa = " WHERE MaKhoa = '" + cboKhoa.Text + "'";
            mhBUS.LayThongTinMonHoc(khoa);
            clsQLSV qlsv = new clsQLSV();
            dataGridViewMH.DataSource = mhBUS.LayThongTinMonHoc(khoa);
        }

        private void btnKhongluu_Click(object sender, EventArgs e)
        {
            KiemTraChinhSua(false);
        }

        private void bntThem_Click(object sender, EventArgs e)
        {
            KiemTraChinhSua(true);
            Reset();
            flags = 1;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flags = 2;
            string temp = txtMaMH.Text;
            KiemTraChinhSua(true);
            txtMaMH.Enabled = false;
            txtMaMH.Text = temp; 
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("Bạn có chắc rằng mình muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
            {
                flags = 3;
                btnLuu_Click(sender, e);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            clsQLMH mhBUS = new clsQLMH();
            mhBUS.MaMH = txtMaMH.Text.Trim();
            mhBUS.TenMH = txtTenMH.Text.Trim();
            mhBUS.TCLT = txttclt.Text.Trim();
            mhBUS.TCTH = txttcth.Text.Trim();
            mhBUS.MaKhoa = cboKhoa2.Text.Trim();
            mhBUS.NgayThi = pickNgaythi.Text.Trim();
            

            if (KiemTraLuu().Equals(""))
            {
                switch (flags)
                {
                    case 1: //Insert
                        mhBUS.ThemThongTinMonHoc();
                        break;
                    case 2: //Update
                        mhBUS.UpdateThongTinMonHoc();
                        break;
                    case 3: //Delete
                        mhBUS.DeleteThongTinMonHoc();
                        break;
                }
                frmQLMH_Load(sender, e);
                KiemTraChinhSua(false);
            }
            else
                MessageBox.Show(KiemTraLuu().ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }
}